#pragma once
#include"Common.h"
#include"ObjectPool.h"
#include"PageMap.h"


// 单例模式 全局唯一
class PageCache
{
public:
	static PageCache* GetInstence()
	{
		return &_owner;
	}

	// 释放空闲span回到Pagecache，并合并相邻的span
	void ReleaseSpanToPageCache(span* span);

	// 获取⼀个K⻚的span 
	span* NewSpan(size_t k);

	std::mutex _pageMtx;   //避免多进程同时访问

	span* MapObjectToSpan(void* obj);

private:
	PageCache() {};
	PageCache(PageCache&) = delete;
	PageCache& operator=(const PageCache&) = delete;

	static PageCache _owner;
	SpanList _spanLists[NPAGES+1]; // 一共128个页映射，0去除

	ObjectPool<span> _spanPool; // 代替new

	
	TCMalloc_PageMap2<32 - PAGE_SHIFT> _idToSpan; // 使用基数树进行映射

	//std::unordered_map<PAGE_ID, span*> _idToSpan; // 要加锁访问
};

